﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using WebKpiPage.Data.GongdanMgr;
using WebKpiPage.Data.Common;
using Model;
namespace WebKpiPage.BLL.GongdanMgr
{
    public class GongdanLogic
    {
        #region 系统自动派单
        /// <summary>
        /// 系统自动派单，
        /// </summary>
        public static void AutoMakeGongdan()
        {
           DataSet ds= GongdanMgrCls.MakeXingNengRecords();
            if (ds!=null&&ds.Tables.Count>0)
            {
                StartWorkFlow(ds.Tables[0]);
            }
        }
        /// <summary>
        /// 开始工作流
        /// </summary>
        /// <param name="dt"></param>
        private static  void StartWorkFlow(DataTable dt)
        {
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string id = dt.Rows[i]["id"].ToString();
                    string userID = dt.Rows[i]["FK_User_责任人"].ToString();
                    ///工作流启动
                    WFGongdanRecord record = new WFGongdanRecord();
                    bool rtn = WFGongdanMgr.Start(id, userID, ref record);
                   if (rtn)
                   {
                       GeneterateEmailAndSend(dt.Rows[i]);
                  
                   }
                }
            }
        }
        private static void GeneterateEmailAndSend(DataRow dr)
        {
            string email = dr["Email"].ToString();
            string msg = dr["小区名称"].ToString()
                + " 告警，告警级别:"
                 + dr["告警级别"].ToString()
                + "告警名称:"
                + dr["告警名称"].ToString()
                + "最早劣化时段"
                + dr["最早劣化时段"].ToString() + "\r\n";
            SendEmai(email, msg);
        }
        private static void SendEmai(string email, string msg)
        {
            // CommonUtil.SendEmail(email, "工单", msg);

        }

        #endregion

        #region 转派工单
        /// <summary>
        /// 继续派单
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="nextUserId"></param>
        /// <param name="workflowInstanceId"></param>
        /// <param name="gongdanID"></param>
        /// <param name="activityId"></param>
        public static int PaiDanContinue(GongDanProj proj)
        {
            /// proj.FK_User_责任人, proj.WorkflowInstanceId, proj.ID, proj.ActivityId
            string eventName = "";
            switch (proj.ActivityId.ToLower())
            {
                case "fankuistate":
                    eventName = "EveContinue";
                    break;
                case "wyzgstate":
                    eventName = "EveWYZGContinue";
                    break;
                case "wyzrstate":
                    eventName = "EveWYZRContinue";
                    break;
                default:
                    break;
            }
            if (eventName.Length > 0)
            {
                return WFGongdanMgr.PingGuFankui(proj, eventName);
            }
            return -1;
           
        }
        #endregion 

        #region 工单打回原指派人
        /// <summary>
        /// 工单打回原指派人
       /// </summary>
       /// <param name="userID"></param>
        /// <param name="preUserId"></param>
       /// <param name="workflowInstanceId"></param>
       /// <param name="gongdanID"></param>
       /// <param name="activityId"></param>
        public static int Reject(GongDanProj proj)
        {
            string eventName = "";
            switch (proj.ActivityId.ToLower())
            {
                case "fankuistate":
                    eventName = "EveReject";
                    break;
                case "wyzgstate":
                    eventName = "EveWYZGReject";
                    break;
                case "wyzrstate":
                    eventName = "EveWYZRReject";
                    break;
                default:
                    break;
            }
            if (eventName.Length > 0)
            {
                return WFGongdanMgr.PingGuFankui(proj, eventName);
            }
            return -1;
          
        }
        #endregion

        #region 处理工单
        /// <summary>
        /// 处理
      /// </summary>
      /// <param name="userID"></param>
      /// <param name="nextUserId"></param>
      /// <param name="workflowInstanceId"></param>
      /// <param name="gongdanID"></param>
      /// <param name="activityId"></param>
        public static int Chuli(GongDanProj proj)
        {
            string eventName = "";
            switch (proj.ActivityId.ToLower())
            {
                case "fankuistate":
                    eventName = "EveTijiao";
                    break;
                case "wyzgstate":
                    eventName = "EveWYZGStateTijiao";
                    break;
                case "wyzrstate":
                    eventName = "EveWYZRStateTijiao";
                    break;
                default:
                    break;
            }
            if (eventName.Length>0)
            {
                ///还是由原来的责任人处理
              return  WFGongdanMgr.PingGuFankui(proj, eventName);
            }
            return -1;
        }
        #endregion

        #region 延时申请，处理
        /// <summary>
        ///  申请延时
     /// </summary>
     /// <param name="userID"></param>
     /// <param name="workflowInstanceId"></param>
     /// <param name="gongdanID"></param>
     /// <param name="activityId"></param>
     /// <param name="time"></param>
     /// <returns></returns>
        public static int RequestMoreTime(GongDanProj proj, int requestDayNums)
        {
            Guid nextUserId = WFWorkHelper.WFActivityToPosition(proj.DelwithMan);
            proj.FK_User_责任人 = nextUserId;
            WFGongdanMgr.requestDayNums = requestDayNums;
            return WFGongdanMgr.PingGuFankui(proj, "EveShQYSh");
        }
       /// <summary>
       ///  申请延时的回复,同意或反对
       /// </summary>
       /// <param name="userID"></param>
       /// <param name="workflowInstanceId"></param>
       /// <param name="gongdanID"></param>
       /// <param name="activityId"></param>
       /// <param name="status">0：反对，1：同意</param>
       /// <returns></returns>
        public static int RequestDelwith(GongDanProj proj, int status)
        {
            
            string eventName = status == 0 ? "EveRejectYSh" : "EveAgreeYSh";
            return WFGongdanMgr.PingGuFankui(proj, eventName);
        }

        #endregion

        #region 评估
        /// <summary>
        /// 获取待评估的工单，决定升级还是关闭
        /// </summary>
        /// <param name="timeInterview"></param>
        /// <param name="status"></param>
        public static void GetPingGuStatsGongdan(object statusObj)
        {
            int status = (int)statusObj;
            int timeInterview = 0;
            if (status==6)
            {
                #warning 需要获取评估设置的时间
                timeInterview = 7;
            }
            else  if (status == 8)
            {
                timeInterview = 20;
            }
            else
            {
                return;
            }
            DataSet ds = GongdanMgrCls.GetPingGuStatsGongdan(timeInterview, status);
            DataTable dt = ds.Tables[0];
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Guid workflowInstanceId = new Guid(dt.Rows[i]["WorkflowInstanceId"].ToString());

                    Guid gongdanID = new Guid(dt.Rows[i]["id"].ToString());
                    string activityId = dt.Rows[i]["ActivityId"].ToString();

                    Guid senderUser = new Guid(dt.Rows[i]["FK_RecUser_ID"].ToString());
                    Guid nextUserId = WFWorkHelper.WFActivityToPosition(senderUser);
                    string eventName = status == 6 ? "EveChuliStateUp" : "EveWYZGPGOK";
                    GongDanProj proj = new GongDanProj();
                    proj.WorkflowInstanceId = workflowInstanceId;
                    proj.DelwithMan=senderUser;
                    proj.FK_User_责任人 = nextUserId;
                    proj.ID=gongdanID;
                    proj.ActivityId=activityId;
                    WFGongdanMgr.PingGuFankui(proj, eventName);
                }
            }
        }
        #endregion 

        #region 未处理超时检查
        /// <summary>
        ///  获取所有超时的工单
        /// </summary>
        /// <param name="interView">超时界限 天</param>
        /// <param name="type">1 超时1 通知主管  2：超时2 通知主任</param>
        public static void GetTimeOutGonddan(int interView,string activeId)
        {
            //获取所有没有处理的工单 
            DataSet ds = GongdanMgrCls.GetTimeOutGonddan(interView, activeId);
            DataTable dt = ds.Tables[0];
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Guid workflowInstanceId = new Guid(dt.Rows[i]["WorkflowInstanceId"].ToString());

                    Guid gongdanID = new Guid(dt.Rows[i]["id"].ToString());
                    string activityId = dt.Rows[i]["ActivityId"].ToString();

                    Guid senderUser = new Guid(dt.Rows[i]["FK_RecUser_ID"].ToString());
                    Guid nextUserId = WFWorkHelper.WFActivityToPosition(senderUser);
                    string eventName = activeId == "fankuistate" ? "EveTimeOut" : "EveTimeOut2";

                    GongDanProj proj = new GongDanProj();
                    proj.WorkflowInstanceId = workflowInstanceId;
                 //   proj.DelwithId = new Guid(dt.Rows[i]["DelwithId"].ToString());
                    proj.DelwithMan = senderUser;
                    proj.FK_User_责任人 = nextUserId;
                    proj.ID = gongdanID;
                    proj.ActivityId = activityId;

                    WFGongdanMgr.PingGuFankui(proj, eventName);
                }
            }
        }
        #endregion
    }
}
